package net.zjitc.dao.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.pojo.User;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22.
 */
public class UserDaoImpl implements UserDao {
    public List<User> findAll() {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from user";
        try {
            List<User> list = (List<User>) runner.query(sql,new BeanListHandler(User.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int findUsername(String username) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from user where username=?";
        int result = 0;
        try {
            User user = runner.query(sql,new BeanHandler<User>(User.class),username);
            if (user != null) result++;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int save(User user) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="insert into user(username,password,name,email,phone,addr,state,code)value(?,?,?,?,?,?,?,?)";
        try {
            int count=runner.update(sql,user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getPhone(),user.getPhone(),user.getAddr(),user.getState(),user.getCode());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User getState(String username, String password) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from user where username=? and password = ?";
        User user = null;
        try {
            user = runner.query(sql,new BeanHandler<User>(User.class),username,password);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User findByCode(String code) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from user where code= ?";
        try {
            User user= (User) runner.query(sql,new BeanListHandler(User.class),code);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(User user) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update user set username=?,password=?,name=?,email=?,phone=?,addr=?,state=?,code=? where uid= ?";
        try {
            int count=runner.update(sql,user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getPhone(),user.getAddr(),user.getState(),user.getCode(),user.getUid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
